<?php

class CC_Helpdesk_ActionParser
{
    /**
     * @var array Actions array
     */
    protected $_actions = array();
    /**
     * @var CC\Entity\Ticket Ticket entity
     */
    protected $_ticket;
    /**
     * @var Doctrine\ORM\EntityManager Doctrine entity manager 
     */
    protected $_em;
    
	/**
     * Contructor
     * 
     * @param string $actionExpression
     */
    public function __construct(Doctrine\ORM\EntityManager $em, $actionExpression = null, CC\Entity\Ticket $ticket = null)
    {
        if($actionExpression !== null && $actionExpression !== '')
        {
            $this->_actions = CC_Inflector::parseParamStringArray($actionExpression);
        }
        $this->_ticket = $ticket;
        $this->_em = $em;
    }
    
    /**
     * 
     */
    public function parse()
    {
        foreach($this->_actions as $action)
        {
            $actionObj = new $action['action']();
            $actionObj->setTicket($this->_ticket)
                      ->setEntityManager($this->_em)
                      ->setParams($action)
                      ->parse();
        }
    }
    
    /**
     * 
     * @param string $expression
     */
    public function setActionExpression($expression)
    {
        $this->_actions = CC_Inflector::parseParamStringArray($expression);
        return $this;
    }
    
    /**
     * 
     * @param CC\Entity\Ticket $ticket Ticket entity
     */
    public function setTicket(CC\Entity\Ticket $ticket)
    {
        $this->_ticket = $ticket;
        return $this;
    }
    
    /**
     * Set the entity manager
     * @param Doctrine\ORM\EntityManager $em
     */
    public function setEntityManager(Doctrine\ORM\EntityManager $em)
    {
        $this->_em = $em;
        return $this;
    }
    
    /**
     * 
     */
    public function getActionExpressions()
    {
        return $this->_actions;
    }
    
    /**
     * 
     */
    public function getTicket()
    {
        return $this->_ticket;
    }
    
    /**
     * Return the doctrine entity manager
     */
    public function getEntityManager()
    {
        return $this->_em;
    }
}
?>